<?php
// +----------------------------------------------------------------------
// | Cframe [ YOU CAN DO IT ]
// +----------------------------------------------------------------------
// | CreateTime 2017年4月24日 17:35:39
// +----------------------------------------------------------------------
// | Function:Memcache类
// +----------------------------------------------------------------------
namespace Cframe\Caches\Types;
use \Memcached as Memd;

class Memcached {
    private $type = 'Memcached'; //连接类型
    private $m;//存储memcached 对话
    private $time = 0; //存储时间
    private $error;  //发送错误
    private $debug = "true";
    public function __construct(){
        if(!class_exists($this->type)){
            $this->error = "No ".$this->type;
            return false;
        }else{
            $this->m = new $this->type;
        }
        $this->mem->connect('localhost',11211) or die ("Could not connect to memcache server");
    }
    
    public function s($key,$value = "",$time = 0){
        $number= func_num_args();//判断传的参数个数
        if($number ==1){
            return $this->get($key);
        }else if($number>= 2){
            if($value=== null){
                //删除操作
                $this->delete($key);
            }else{
                //set操作
                $this->set($key,$value,$time);
            }
        }
    }
    
    public function set($key,$value,$time){
        if($time===null){
            $time = $this->time;
        }
        $this->m->set($key,$value,$time);
        if($this->debug){
            if($this->m->getResultCode()!=0){
                return false;
            }
        }
    }
    
    private function get($key){
        $return =  $this->m->get($key);
        if($this->debug){
            if($this->m->getResultCode()!=0){
                return false;
            }else{
                return $return;
            }
        }

    }
    
    private function delete($key){
        $this->m->delete($key);
    }
    
    
    public function getError(){
        if($this->error){
            return $this->error;
        }else{
            return $this->m->getResultMessage();
        }
    }
}

